
//document.write("<script src='libs/jweixin-1.0.0.js'><\/script>");

function createDOMElementVideo  () {
    // Lets find the parent HTML Element (we will add our media element to the end of this element as a child element)
    var idRef = "container";
    // Lets make sure there is something here
    //var lParent = $(idRef)[0];
    var lParent = document.getElementById(idRef);
    //console.log(lParent)
    //console.log(lParent);
    if (!lParent) {
        var lMesg = "没有元素 [" + idRef + "] ";
        return null;
    }

    var videoHolder = document.createElement("div");
    videoHolder.id = "videobox";

    //console.log(videoHolder)
    lParent.appendChild(videoHolder);


    var jweixin = document.createElement("script");

    jweixin.src = "libs/jweixin-1.0.0.js";
    videoHolder.appendChild(jweixin);
    // lets dynamically add a video element
    var lVideo = document.createElement("video");

    lVideo.id="video";
    if(browser.versions.ios){
        //console.log("ios")
        lVideo.setAttribute("playsinline","true");
        lVideo.setAttribute("autoplay","true");
        lVideo.setAttribute("preload","auto");
        lVideo.style.width = "100%";

     }else if(browser.versions.android){
        //console.log("an")
        lVideo.setAttribute("x5-video-player-type","h5");
        lVideo.setAttribute("autoplay","true");
        lVideo.setAttribute("preload","auto");
        lVideo.setAttribute("webkit-playsinline","true");
        lVideo.setAttribute("controls","false");
        lVideo.setAttribute("x5-video-player-fullscreen","false");
        lVideo.style.width = "100%";
        lVideo.style.height = "100%";
        lVideo.style.backgroundImage = "url('imgs/blackbg.png')";

     }else{
        //console.log("other")
        lVideo.setAttribute("playsinline","true");
        lVideo.setAttribute("autoplay","true");
        lVideo.setAttribute("preload","auto");
        lVideo.style.width = "100%";
        lVideo.style.height = "100%";

     }

    var self = this; // Store a reference to this
    lVideo.addEventListener("error", function (evt) {
        var lErrMesg = "Error::SWElementVideo:: Error loading video element, event.type [" + evt.type + "] Media Details: [" + evt.target.src + "]";
        console.log(lErrMesg);
    });

    //if(cur_page == PAGE_2){
    //    var video = document.getElementById("video")
    //lVideo.setAttribute("src","videos/20S.mp4");
    //VIDEO_LOADED = true;
    //}
    //videoHolder.style.display = "block";

    videoHolder.appendChild(lVideo);
    videoHolder.style.display = "none";


}
createDOMElementVideo();


if(is_weixin()){
    if (typeof WeixinJSBridge == "undefined") {
        var i = 0;

        if (document.addEventListener) {

            document.addEventListener("WeixinJSBridgeReady", function func() {

                var video = document.getElementById("video");

                if(video != null){
                    i = null;
                    //video.pause();
                    video.play();
                    var viceoHolder = document.getElementById("videobox");
                    //viceoHolder.style.display = "block";
                    if(browser.versions.ios){
                        video.style.top = "-50px";
                    }

                    video.addEventListener('ended',function (){
                        console.log("ended");
                        setAction();
                        viceoHolder.style.display = "none";
                        //var canvas = document.getElementById("canvas");
                        //canvas.style.zIndex ="1000";
                        //contentLayer.createPage(0,true);


                    })
                    video.addEventListener('timeupdate',function (){
                        if ( !video.isPlayed && video.currentTime>AUTO_VIDEO.end_time ){
                            //video.pause();
                            video.setAttribute("x5-video-player-fullscreen","false");
                            viceoHolder.style.display = "none";

                        }
                    })
                } else {
                    if (i++ < 10) {
                        setTimeout(func, 100);
                    }
                }
            }, false);
        }
    }
}else{

    var video = document.getElementById("video");
    var viceoHolder = document.getElementById("videobox");
    if(video != null) {
        video.play();
        //viceoHolder.style.display = "block";
    }
    video.addEventListener('ended',function (){
        //console.log("ended");
        setAction();
        //video.pause();
        viceoHolder.style.display = "none";
        //contentLayer.createPage(0,true);


    })
}

/*
 * 设置action
 * */
function setAction(){
    var action = VideoPage.action;
    if(action){
        switch (action.type){
            case "popup"://弹窗s
                popupLayer.createPopup(action.group_id);
                break;
            case "jumpto"://跳转
                contentLayer.jumpTo(action.page);
                break;
            case "jumptoURL"://跳转外链
                window.location =action.url;
                break;
        }


    }
}

